/*     */ package com.zimbra.cs.mailbox.util;
/*     */ 
/*     */ import com.google.common.base.Joiner;
/*     */ import com.google.common.base.Splitter;
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.Lists;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.ArrayUtil;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.Tag;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TagUtil
/*     */ {
/*  40 */   private static final String[] NO_TAGS = new String[0];
/*     */   
/*     */   @Deprecated
/*     */   public static String[] tagIdStringToNames(Mailbox mbox, OperationContext octxt, String tagIdString) throws ServiceException {
/*  44 */     if (Strings.isNullOrEmpty(tagIdString)) {
/*  45 */       return NO_TAGS;
/*     */     }
/*     */     
/*  48 */     List<String> tags = Lists.newArrayList();
/*  49 */     for (String tagId : Splitter.on(',').omitEmptyStrings().trimResults().split(tagIdString)) {
/*     */       try {
/*  51 */         tags.add(mbox.getTagById(octxt, Integer.parseInt(tagId)).getName());
/*     */       } catch (NumberFormatException nfe) {
/*  53 */         throw ServiceException.INVALID_REQUEST("invalid tag ID string: " + tagIdString, nfe);
/*     */       }
/*     */     }
/*  56 */     return (String[])tags.toArray(new String[tags.size()]);
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public static String getTagIdString(MailItem item) {
/*  61 */     return getTagIdString(item.getMailbox(), item.getTags());
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public static String getTagIdString(Mailbox mbox, String[] tags) {
/*  66 */     return Joiner.on(',').join(getTagIds(mbox, tags));
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public static List<Integer> getTagIds(MailItem item) {
/*  71 */     return getTagIds(item.getMailbox(), item.getTags());
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public static List<Integer> getTagIds(Mailbox mbox, String[] tags) {
/*  76 */     if ((tags == null) || (tags.length == 0)) {
/*  77 */       return Collections.emptyList();
/*     */     }
/*     */     
/*  80 */     List<Integer> tagIds = new ArrayList(tags.length);
/*  81 */     for (String tag : tags) {
/*     */       try {
/*  83 */         tagIds.add(Integer.valueOf(mbox.getTagByName(null, tag).getId()));
/*     */       } catch (ServiceException e) {}
/*     */     }
/*  86 */     if (tagIds.size() > 1) {
/*  87 */       Collections.sort(tagIds);
/*     */     }
/*  89 */     return tagIds;
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public static String[] tagBitmaskToNames(Mailbox mbox, OperationContext octxt, long tagBitmask) throws ServiceException {
/*  94 */     if (tagBitmask == 0L) {
/*  95 */       return NO_TAGS;
/*     */     }
/*     */     
/*  98 */     List<String> tags = Lists.newArrayList();
/*  99 */     for (int i = 0; i <= 31; i++) {
/* 100 */       long mask = 1L << i;
/* 101 */       if ((tagBitmask & mask) != 0L) {
/* 102 */         tags.add(mbox.getTagById(octxt, 64 + i).getName());
/*     */       }
/*     */     }
/* 105 */     return (String[])tags.toArray(new String[tags.size()]);
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public static String tagIdToName(Mailbox mbox, OperationContext octxt, int tagId) throws ServiceException {
/* 110 */     return mbox.getTagById(octxt, tagId).getName();
/*     */   }
/*     */   
/*     */   public static String encodeTags(String... tags) {
/* 114 */     return (tags == null) || (tags.length == 0) ? "" : encodeTags(Arrays.asList(tags));
/*     */   }
/*     */   
/*     */   public static String encodeTags(Collection<String> tags) {
/* 118 */     if ((tags == null) || (tags.isEmpty())) {
/* 119 */       return "";
/*     */     }
/*     */     
/* 122 */     StringBuilder encoded = new StringBuilder();
/* 123 */     for (String tag : tags) {
/* 124 */       encoded.append(encoded.length() == 0 ? "" : ",").append(tag.replace("\\", "\\\\").replace(",", "\\,"));
/*     */     }
/* 126 */     return encoded.toString();
/*     */   }
/*     */   
/* 129 */   private static final Splitter TAG_SPLITTER = Splitter.on(',').omitEmptyStrings().trimResults();
/*     */   
/*     */   public static String[] decodeTags(String encoded) {
/* 132 */     if (Strings.isNullOrEmpty(encoded))
/* 133 */       return NO_TAGS;
/* 134 */     if (encoded.indexOf('\\') == -1) {
/* 135 */       List<String> tags = Lists.newArrayList(TAG_SPLITTER.split(encoded));
/* 136 */       return (String[])tags.toArray(new String[tags.size()]);
/*     */     }
/* 138 */     List<String> tags = Lists.newArrayList();
/* 139 */     StringBuilder sb = new StringBuilder(encoded.length());
/* 140 */     boolean escaped = false;
/* 141 */     int i = 0; for (int len = encoded.length(); i < len; i++) {
/* 142 */       char c = encoded.charAt(i);
/* 143 */       if (escaped) {
/* 144 */         sb.append(c);
/* 145 */         escaped = false;
/* 146 */       } else if (c == '\\') {
/* 147 */         escaped = true;
/* 148 */       } else if (c == ',') {
/* 149 */         String tag = sb.toString().trim();
/* 150 */         if (!tag.isEmpty()) {
/* 151 */           tags.add(tag);
/*     */         }
/* 153 */         sb.setLength(0);
/*     */       } else {
/* 155 */         sb.append(c);
/*     */       }
/*     */     }
/* 158 */     String tag = sb.toString().trim();
/* 159 */     if (!tag.isEmpty()) {
/* 160 */       tags.add(tag);
/*     */     }
/* 162 */     return (String[])tags.toArray(new String[tags.size()]);
/*     */   }
/*     */   
/*     */   public static boolean tagsMatch(String[] tags1, String[] tags2)
/*     */   {
/* 167 */     if (tags1 == tags2)
/* 168 */       return true;
/* 169 */     if (ArrayUtil.isEmpty(tags1))
/* 170 */       return ArrayUtil.isEmpty(tags2);
/* 171 */     if (ArrayUtil.isEmpty(tags2)) {
/* 172 */       return false;
/*     */     }
/* 174 */     Set<String> s1 = Sets.newHashSet(tags1);Set<String> s2 = Sets.newHashSet(tags2);
/* 175 */     return (s1.size() == s2.size()) && (!s1.retainAll(s2));
/*     */   }
/*     */   
/*     */   public static String[] parseTags(Element elt, Mailbox mbox, OperationContext octxt) throws ServiceException
/*     */   {
/* 180 */     String tags = elt.getAttribute("tn", null);
/* 181 */     if (tags != null) {
/* 182 */       return decodeTags(tags);
/*     */     }
/*     */     
/* 185 */     String tagIds = elt.getAttribute("t", null);
/* 186 */     if (tagIds != null) {
/* 187 */       return tagIdStringToNames(mbox, octxt, tagIds);
/*     */     }
/*     */     
/* 190 */     return null;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/util/TagUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */